home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 095 / conio.c < prev    next >
Text File  |  1985-06-03  |  4KB  |  155 lines

  1.  
  2. /**
  3. *
  4. * This module defines the various console I/O functions.  They may
  5. * be called directly, using the names included here, or the header
  6. * file CONIO.H may be included so that more standard names may be
  7. * used.  This source module is provided so that users may customize
  8. * the console I/O functions, if desired.  Note that "cprintf" and
  9. * "cscanf" (included in MC.LIB) call the functions "putch", "getch",
  10. * and "ungetch".
  11. *
  12. **/
  13. #define BDOS_IN   7    /* input function for "getch" */
  14. #define BDOS_OUT  6    /* output function for "putch" */
  15. #define BDOS_CKS  11    /* check keyboard status for "kbhit" */
  16. #define BDOS_BKI  10    /* buffered keyboardd input for "cgets" */
  17. #define BDOS_PRT  9    /* print string for "cputs" */
  18.  
  19. static char pushback;    /* character save for "ungetch" */ 
  20.  
  21. /**
  22. * name        getch -- get character from console
  23. *
  24. * synopsis    c = getch();
  25. *        char c;        input character
  26. *
  27. * description    This function obtains the next character typed at
  28. *        the console or, if one was pushed back via "ungetch",
  29. *        returns the previously pushed back character.
  30. *
  31. **/
  32. getch()
  33. {
  34. int c;
  35.  
  36. if (pushback != '\0')
  37.    {            /* character was pushed back */
  38.    c = pushback;
  39.    pushback = '\0';
  40.    return(c);
  41.    }
  42. return(bdos(BDOS_IN, 0xFF) & 127);
  43. }
  44. /**
  45. *
  46. * name        putch -- send character directly to console
  47. *
  48. * synopsis    putch(c);
  49. *        char c;        character to be sent
  50. *
  51. * description    This function sends the specified character directly
  52. *        to the user's console.
  53. *
  54. **/
  55. putch(c)
  56. char c;
  57. {
  58. bdos(BDOS_OUT, c&127);
  59. return(c);
  60. }
  61. /**
  62. *
  63. * name        ungetch -- push character back to console
  64. *
  65. * synopsis    r = ungetch(c);
  66. *        int r;        return code
  67. *        char c;        character to be pushed back
  68. *
  69. * description    This function pushes the indicated character back
  70. *        on the console.  Only a single level of pushback is
  71. *        allowed.  The effect is to cause "getch" to return
  72. *        the pushed-back character the next time it is called.
  73. *
  74. * returns    r = -1 if character already pushed back
  75. *        = c otherwise
  76. *
  77. **/
  78. ungetch(c)
  79. char c;
  80. {
  81.  
  82. if (pushback != '\0') return(-1);
  83. pushback = c;
  84. return(c);
  85. }
  86. /**
  87. *
  88. * name        cgets -- get string directly from console
  89. *
  90. * synopsis    p = cgets(s);
  91. *        char *p;    pointer to result string
  92. *        char *s;    string buffer (first byte = count)
  93. *
  94. * description    This function obtains a string directly from the
  95. *        user's console.  This version uses the buffered
  96. *        keyboard input function supported by the BDOS, so
  97. *        that all of the line editing capabilities are available.
  98. *        The first byte of "s" must be initialized to contain
  99. *        the number of bytes, minus two, in "s".  The string
  100. *        pointer returned is "s+2", which contains the first
  101. *        byte of input data.  Note that "s[1]" will contain
  102. *        the number of characters in the string.  The carriage
  103. *        return (which the user at the console must type to
  104. *        terminate the operation) is replaced by a null byte.
  105. *
  106. * returns    p = pointer to string received
  107. *
  108. **/
  109. char *cgets(s)
  110. char *s;
  111. {
  112. char *p;
  113.  
  114. if (*s == 0) *s = 250;        /* do not allow zero byte count */
  115. bdos(BDOS_BKI, s);
  116. p = s+2;
  117. p[s[1]] = '\0';            /* set terminating byte */
  118. return(p);
  119. }
  120. /**
  121. *
  122. * name        cputs -- send character string directly to console
  123. *
  124. * synopsis    cputs(s);
  125. *        char *s;    character string to be sent
  126. *
  127. * description    This function sends the specified string directly to
  128. *        the user's console.  The BDOS function for "print
  129. *        string" is used.  The function locates the terminating
  130. *        null byte, changes it to a '$' (the terminator 
  131. *        required by the BDOS function), and then changes it
  132. *        back to the null byte before returning.  Thus, the
  133. *        string to be printed cannot itself contain a '$' and
  134. *        it cannot reside in read-only memory (ROM).
  135. *
  136. *        Note that a carriage return or linefeed is NOT appended
  137. *        by this function; they must be included in the string,
  138. *        if desired.
  139. *
  140. **/
  141. cputs(s)
  142. char *s;
  143. {
  144. char *p;
  145.  
  146. for (p = s; *p != '\0'; p++) ;        /* find string terminator */
  147. *p = '$';
  148. bdos(BDOS_PRT, s);
  149. *p = '\0';
  150. return;
  151. }
  152. ar *p;
  153.  
  154. for (p = s; *p != '\0'; p++)